import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description: 有效三角形个数
 * 力扣
 * User: Spider-Man
 * Date: 2025-03-12
 * Time: 22:44
 */
public class EffectiveTriangle {
    /***
     * 暴力枚举
     * @param nums
     * @return
     */
    public static int triangleNumber1(int[] nums) {
        int count = 0;
        Arrays.sort(nums);
        for(int i = 0; i < nums.length; i++) {
            for(int j = i+1; j < nums.length; j++) {
                for(int k = j+1; k < nums.length; k++) {
                    if(nums[i] + nums[j] > nums[k])
                        count++;
                }
            }
        }
        return count;
    }

    /***
     * 单调性 双指针
     */
    public static int triangleNumber2(int[] nums) {
        Arrays.sort(nums);
        int count = 0;
        for(int k = nums.length - 1; k >= 2; k--) {
            int left = 0;
            int right = k-1;
            while(left < right) {
                if(nums[left] + nums[right] > nums[k]) {
                    count += right - left;
                    right--;
                } else  {
                    left++;
                }
            }
        }
        return count;
    }
    public static void main(String[] args) {
        int[] nums = {0,1,1};
        //System.out.println(triangleNumber1(nums));
        System.out.println(triangleNumber2(nums));
    }
}
